package com.jeeplus.test.education.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.sys.service.OfficeService;
import com.jeeplus.test.education.domain.vo.EducationInfoVo;
import com.jeeplus.test.education.mapper.EducationInfoMapper;
import com.jeeplus.test.education.service.EducationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;

/**
 * 继续教育学院管理服务实现层 - 教育服务具体实现
 ** 包含与数据访问层交互、事务管理等核心功能实现
 *
 * @author 马贵东
 * @since 2025-03-05
 */
@Service
public class EducationServiceImpl implements EducationService {

    @Autowired
    private EducationInfoMapper educationInfoMapper;

    @Autowired
    private OfficeService officeService;

    @Override
    public Page<Map<String, Object>> getEducationInfoPage(EducationInfoVo educationInfoVo, Page<EducationInfoVo> page) {

        if(StringUtils.isNotEmpty(educationInfoVo.getOfficeId())){
            //获取机构下所有层级
            List<String> officesList = officeService.getOfficesTreeByParentId(educationInfoVo.getOfficeId());
            officesList.add(educationInfoVo.getOfficeId());
            educationInfoVo.setOfficesList(officesList);
        }
        // 处理type
        if (StringUtils.isNotEmpty(educationInfoVo.getApproveState())) {
            String[] arr = educationInfoVo.getApproveState().split(",");
            List<String> list = Arrays.asList(arr);
            educationInfoVo.setApproveStateList(list);
        }

        // 1. 获取分页结果和字段配置信息
        Page<EducationInfoVo> educationInfoPage = educationInfoMapper.getEducationInfoPage(educationInfoVo, page);
        List<EducationInfoVo> educationList = educationInfoPage.getRecords();
        List<Map<String, Object>> modifiedRecords = new ArrayList<>();

        // 2. 遍历每条记录
        for (EducationInfoVo record : educationList) {
            Map<String, Object> recordMap = new HashMap<>();

            // 3. 将数据放入 map
            Field[] fields = EducationInfoVo.class.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    // 获取字段值并添加到 map 中
                    Object value = field.get(record);
                    recordMap.put(field.getName(), value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

            // 5. 添加处理后的记录
            modifiedRecords.add(recordMap);
        }

        // 6. 将处理后的记录封装到新的分页对象中返回
        Page<Map<String, Object>> modifiedPage = new Page<>(educationInfoPage.getCurrent(), educationInfoPage.getSize(), educationInfoPage.getTotal());
        modifiedPage.setRecords(modifiedRecords);

        return modifiedPage;
    }


    @Override
    public void update(EducationInfoVo educationInfoVo) {

        educationInfoMapper.updateEducation(educationInfoVo);

    }
}
